//
//  ChangeZFViewController.m
//  Vpay
//
//  Created by 伟大大 on 2018/9/29.
//  Copyright © 2018年 丁伟佳. All rights reserved.
//

#import "ChangeZFViewController.h"
#import "LoginViewController.h"
#import "ForgetZFPassViewController.h"
@interface ChangeZFViewController ()

@end

@implementation ChangeZFViewController

#pragma mark -
#pragma mark - for lifeCircle
#pragma mark -
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    //    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改支付密码";

    self.view = [[[NSBundle mainBundle]loadNibNamed:@"ChangeZFViewController" owner:self options:nil]lastObject];
    self.view.backgroundColor = BACKCOLOR;
    
    
    
    //    [self initData];
    [self initNavBtns];
    [self initViews];
    
    //    [NSThread detachNewThreadSelector:@selector(requestData) toTarget:self withObject:nil];
    
    //    [NSThread detachNewThreadSelector:@selector(requestDingdanData) toTarget:self withObject:nil];
}
#pragma mark -
#pragma mark - for Views
#pragma mark -

- (void)initNavBtns {
    
    self.navigationItem.leftBarButtonItem = [AppUtility getNavBtnItemLefBacktWith:@selector(leftNavBtnClicked) andTarget:self];
}
- (void)initViews {
    
    self.forgetzf_btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

    self.Confirm_btn.layer.cornerRadius = 4.5;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapAction)];
    [self.view addGestureRecognizer:tap];
}
#pragma mark -
#pragma mark for evevts
#pragma mark -
- (void)leftNavBtnClicked {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)TapAction {
    
    if ([_oldzf_textfiled isFirstResponder]) {
        [_oldzf_textfiled resignFirstResponder];
    }
    if ([_newzf_textfiled isFirstResponder]) {
        [_newzf_textfiled resignFirstResponder];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc {
    [_oldzf_textfiled release];
    [_newzf_textfiled release];
    [_forgetzf_btn release];
    [_Confirm_btn release];
    [super dealloc];
}
- (IBAction)forgetzf_btnClik:(UIButton *)sender {
    ForgetZFPassViewController *vc = [[ForgetZFPassViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)Confirm_btnClik:(UIButton *)sender {
    if ([AppUtility isBlankString:_oldzf_textfiled.text]) {
        //        [self.view makeToast:NSLocalizedString(@"oldpasskong", nil)];
        
        [self.view makeToast:@"旧支付密码不能为空" duration:1.0 position:@"center" title:nil image:[AppUtility getLocalImage:@"error"]];
        return;
    }
    if ([AppUtility isBlankString:_newzf_textfiled.text]) {
        //        [self.view makeToast:NSLocalizedString(@"newpasskong", nil)];
        [self.view makeToast:@"新支付密码不能为空" duration:1.0 position:@"center" title:nil image:[AppUtility getLocalImage:@"error"]];
        
        return;
    }
    if ([_oldzf_textfiled.text length]<6) {
        
        //        [self.view makeToast:NSLocalizedString(@"oldpassts", nil)];
        [self.view makeToast:@"旧支付密码只能为6位数字" duration:1.0 position:@"center" title:nil image:[AppUtility getLocalImage:@"error"]];
        
        return;
        
    }
    if ([_newzf_textfiled.text length]<6) {
        
        //        [self.view makeToast:NSLocalizedString(@"newpassts", nil)];
        [self.view makeToast:@"新支付密码只能为6位数字" duration:1.0 position:@"center" title:nil image:[AppUtility getLocalImage:@"error"]];
        
        return;
        
    }
    if (![self inputShouldNumber:_oldzf_textfiled.text]) {
        //        [self.view makeToast:NSLocalizedString(@"oldpassts", nil)];
        [self.view makeToast:@"旧支付密码只能为6位数字" duration:1.0 position:@"center" title:nil image:[AppUtility getLocalImage:@"error"]];
        
        return;
    }
    if (![self inputShouldNumber:_newzf_textfiled.text]) {
        //        [self.view makeToast:NSLocalizedString(@"newpassts", nil)];
        [self.view makeToast:@"新支付密码只能为6位数字" duration:1.0 position:@"center" title:nil image:[AppUtility getLocalImage:@"error"]];
        
        return;
    }
    NSString *postStr = @"";
    
    postStr = [NSString stringWithFormat:@"userid=%@&pwd=%@&pwdrpt=%@&pwdtype=0",@"",_oldzf_textfiled.text,_newzf_textfiled.text];
    
    NSDictionary *resultDict =[NetUtility getDataWithURLStr:KURLXiuGaiMiMa andPostStr:postStr andView:self.navigationController.view];
    
    UEXNSLog(@"kURL==%@%@ resultDict=%@",KURLXiuGaiMiMa,postStr,resultDict);
    
    if (resultDict) {
        int stauts = [[resultDict objectForKey:@"s"] intValue];
        
        if (stauts==1) {
            
            //            NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary: [AccountController getAccountInfo]];
            //
            //            [dic setObject:textField.text forKey:@"nickname"];
            //            [AccountController setAccountInfo:dic];
            
            [self.navigationController popViewControllerAnimated:YES];
            
            [self.navigationController.view makeToast:[resultDict objectForKey:@"i"]];

        }else if (stauts == 303) {
            
            LoginViewController *vc = [[LoginViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
        
                [self.navigationController.view makeToast:[resultDict objectForKey:@"i"]];
    }
}
- (BOOL)inputShouldNumber:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex =@"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}
@end
